#!/usr/bin/env jruby
# -*- coding: us-ascii -*-

require 'test/unit'
dir = File.dirname(__FILE__)
require File.join(dir, '..', '..', 'lib', 'auxil', 'htmlstripper')

########################################

module Constants4HTMLStripperTests
  START = <<-start
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
  <HTML>
  <HEAD>
  <TITLE \r\nLANG="FR">\r\nBonjour!\r\n</TITLE>
  <style type="text/css"><!--
  body { font-family: cursive; font-size: large; }
   --></style>
  start
  
  ICKY = <<-icky
  </head>
  <body link=#0000FF\nvlink=#FF00FF>
  <script\nlanguage="Javascript">
  if (5 > 3) { document.write("5 > 3"); }
  </script>
  <h1>Greetings</h1>
  <p>
  <script>\ndocument.write("Hello, moron!");\n</script>
  Hello, friend!
  </p>
  </body>
  </html>
  icky
  
  FULL = START + ICKY
  
  EMPTY = ''
  
  PLAIN = <<-plain
  The boy stood on the burning deck
  His feet were filled with blisters
  plain
end

########################################

class TestHTMLStripperTitle < Test::Unit::TestCase
  include Constants4HTMLStripperTests
  
  def test_should_return_title_content_only
    result = HTMLStripper.title(START)
    assert_equal 'Bonjour!', result
    assert_no_match %r'DOCTYPE|strict', result
  end
  
  def test_should_return_empty_for_nonhtml
    assert_equal '', HTMLStripper.title(EMPTY), 'empty'
    assert_equal '', HTMLStripper.title(PLAIN), 'plain'
  end
end

########################################

class TestHTMLStripperBody < Test::Unit::TestCase
  include Constants4HTMLStripperTests
  
  ICKY_RESULT = HTMLStripper.body(ICKY)
  
  def test_should_remove_pre_and_post_body_content
    assert_no_match %r'</head>', ICKY_RESULT
    assert_no_match %r'<body|</body>', ICKY_RESULT
    assert_no_match %r'00FF', ICKY_RESULT
    assert_no_match %r'</html>', ICKY_RESULT
  end
  
  def test_should_keep_body_content
    assert_match %r'<script', ICKY_RESULT
    assert_match %r'h1>', ICKY_RESULT
    assert_match %r'moron', ICKY_RESULT
    assert_match %r'friend', ICKY_RESULT
  end
  
  def test_should_return_empty_for_nonhtml
    assert_equal '', HTMLStripper.body(EMPTY), 'empty'
    assert_equal '', HTMLStripper.body(PLAIN), 'plain'
  end
end

########################################

class TestHTMLStripperDeleteScripts < Test::Unit::TestCase
  include Constants4HTMLStripperTests
  
  FULL_RESULT = HTMLStripper.delete_scripts(FULL)
  
  def test_should_remove_script_tags
    assert_no_match %r'<script|</script>', FULL_RESULT
    assert_no_match %r'Javascript', FULL_RESULT
  end
  
  def test_should_remove_script_content
    assert_no_match %r'document', FULL_RESULT
  end
  
  def test_keep_all_but_scripts
    assert_match %r'DOCTYPE', FULL_RESULT
    assert_match %r'Greetings', FULL_RESULT
    assert_match %r'friend', FULL_RESULT
  end
  
  def test_should_return_original_for_nonhtml
    assert_equal EMPTY, HTMLStripper.delete_scripts(EMPTY)
    assert_equal PLAIN, HTMLStripper.delete_scripts(PLAIN)
  end
end

########################################

class TestHTMLStripperDeleteStyles < Test::Unit::TestCase
  include Constants4HTMLStripperTests
  
  START_RESULT = HTMLStripper.delete_styles(START)
  
  def test_should_remove_style_tags
    assert_no_match %r'<style|</style>', START_RESULT
    assert_no_match %r'css', START_RESULT
  end
  
  def test_should_remove_style_content
    assert_no_match %r'font-family', START_RESULT
  end
  
  def test_keep_all_but_styles
    assert_match %r'DOCTYPE', START_RESULT
    assert_match %r'Bonjour!', START_RESULT
    assert_match %r'HTML', START_RESULT
  end
  
  def test_should_return_original_for_nonhtml
    assert_equal EMPTY, HTMLStripper.delete_styles(EMPTY)
    assert_equal PLAIN, HTMLStripper.delete_styles(PLAIN)
  end
end

########################################

class TestHTMLStripperDeleteTags < Test::Unit::TestCase
  include Constants4HTMLStripperTests
  
  FULL_RESULT = HTMLStripper.delete_tags(FULL)
  
  def test_should_remove_all_tags
    assert_no_match %r'DOCTYPE', FULL_RESULT
    assert_no_match %r'link', FULL_RESULT
    assert_no_match %r'h1', FULL_RESULT
  end
  
  def test_should_keep_all_but_tags
    assert_match %r'moron', FULL_RESULT
    assert_match %r'friend', FULL_RESULT
  end
  
  def test_should_return_original_for_nonhtml
    assert_equal EMPTY, HTMLStripper.delete_tags(EMPTY)
    assert_equal PLAIN, HTMLStripper.delete_tags(PLAIN)
  end
end

########################################

class TestHTMLStripperAllText < Test::Unit::TestCase
  include Constants4HTMLStripperTests
  
  FULL_RESULT = HTMLStripper.all_text(FULL)
  
  def test_should_remove_all_tags
    assert_no_match %r'DOCTYPE', FULL_RESULT
    assert_no_match %r'css|cursive', FULL_RESULT
    assert_no_match %r'head|body|script|h1|p>|html', FULL_RESULT
  end
  
  def test_should_remove_script_content
    assert_no_match %r'5 > 3', FULL_RESULT
    assert_no_match %r'moron', FULL_RESULT
  end
  
  def test_should_keep_title_content
    start_result = HTMLStripper.all_text(START)
    assert_match %r'Bonjour!', start_result
  end
  
  def test_should_keep_body_text
    icky_result = HTMLStripper.all_text(ICKY)
    assert_match %r'Greetings', icky_result
    assert_match %r'Hello, friend!', icky_result
  end
  
  def test_should_keep_title_and_body_when_both
    assert_match %r'Bonjour!', FULL_RESULT
    assert_match %r'Greetings', FULL_RESULT
    assert_match %r'Hello, friend!', FULL_RESULT
  end
  
  def test_should_return_original_for_nonhtml
    assert_equal EMPTY, HTMLStripper.all_text(EMPTY)
    assert_equal PLAIN, HTMLStripper.all_text(PLAIN)
  end
end
